<template>
  <div>
    <el-tree
      ref="tree"
      :data="permissonList"
      show-checkbox
      default-expand-all

      :check-strictly="true"
      node-key="id"
      :props="{ label: 'name'}"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button size="mini" type="primary" @click="hSubmit">确定</el-button>
      <el-button size="mini" @click="hCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { addSonDepartments } from '@/utils'
import { assignPerm, getRolesDetail } from '@/api/setting'

export default {
  name: 'VueHrSecondAssignPermission',
  props: {
    perId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissonList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.tree.getCheckedKeys()
      const res = await assignPerm({ id: this.perId, permIds })
      console.log(res, 'assignPerm')
      this.$emit('success')
    },
    hCancel() {
      this.$emit('close')

      // 清空当前的选择
      this.$refs.tree.setCheckedKeys([])
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        const detail = await getRolesDetail(this.perId)
        console.log('getPermissionList', res)
        console.log('getRolesDetail', detail)
        this.permissonList = addSonDepartments(res.data)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
